﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace FySystem.Net.FyNetConnection.Src
{
    public class StateObjectBase : IDisposable
    {
        /// <summary>
        /// 对于长连接而言，需要有一个标记用于标识这个连接，对于短链接就建议不要了，每次都生成一个GUID浪费性能
        /// </summary>
        public string GUID { get; internal set; }

        public Socket WorkSocket = null;

        public int BufferSize = 1024;

        public byte[] Buffer = null;

        public StateObjectBase(int size = 1024, bool genUId = false)
        {
            BufferSize = size;
            Buffer = new byte[BufferSize];
            if (genUId)
                GUID = Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// 收到的数据
        /// </summary>
        public byte[] stream = null;

        /// <summary>
        /// 上次收到数据的时间
        /// </summary>
        public DateTime LastReceivedTime = DateTime.Now;

        public void Dispose()
        {
            try
            {
                if (WorkSocket != null)
                {
                    WorkSocket.Close();
                    WorkSocket.Dispose();
                }
            }
            catch { }
        }
    }
}
